package com.firefly.admin.workbench.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.firefly.admin.common.enums.ResultCodeEnum;
import com.firefly.admin.common.exception.BusinessException;
import com.firefly.admin.common.utils.DateUtil;
import com.firefly.admin.common.utils.JwtTokenUtil;
import com.firefly.admin.common.vo.Result;
import com.firefly.admin.system.entity.FySystemMenu;
import com.firefly.admin.system.entity.FySystemRole;
import com.firefly.admin.system.service.FySystemMenuService;
import com.firefly.admin.system.service.FySystemRoleService;
import com.firefly.admin.workbench.entity.FySystemShortcutMenu;
import com.firefly.admin.workbench.mapper.FySystemShortcutMenuMapper;
import com.firefly.admin.workbench.service.FySystemShortcutMenuService;
import com.firefly.admin.workbench.vo.ShortcutMenuUpdateVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class FySystemShortcutMenuServiceImpl implements FySystemShortcutMenuService {
    @Autowired
    private FySystemShortcutMenuMapper fySystemShortcutMenuMapper;
    @Autowired
    private FySystemMenuService fySystemMenuService;
    @Autowired
    private FySystemRoleService fySystemRoleService;

    /**
     * 查询工作台快捷菜单列表接口
     *
     * @return 列表
     */
    @Override
    public Result<List<FySystemShortcutMenu>> getShortcutMenuList() {
        List<FySystemShortcutMenu> fySystemShortcutMenuList = new ArrayList<>();
        Long userId = JwtTokenUtil.getLoginUserId();
        List<FySystemRole> roleList = fySystemRoleService.getRoleList(userId);
        if (CollectionUtils.isEmpty(roleList)) {
            return Result.success(fySystemShortcutMenuList);
        }
        List<FySystemMenu> systemMenuList = fySystemMenuService.getMenuListByRoleIds(roleList.stream().map(FySystemRole::getRoleId).collect(Collectors.toList()));
        if (CollectionUtils.isEmpty(systemMenuList)) {
            return Result.success(fySystemShortcutMenuList);
        }
        LambdaQueryWrapper<FySystemShortcutMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FySystemShortcutMenu::getUserId, userId);
        queryWrapper.in(FySystemShortcutMenu::getMenuId, systemMenuList.stream().map(FySystemMenu::getMenuId).collect(Collectors.toList()));
        queryWrapper.orderByAsc(FySystemShortcutMenu::getSort);
        queryWrapper.orderByDesc(FySystemShortcutMenu::getCreateTime);
        fySystemShortcutMenuList = fySystemShortcutMenuMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(fySystemShortcutMenuList)) {
            Map<Long, FySystemMenu> systemMenuMap = systemMenuList.stream().collect(Collectors.toMap(FySystemMenu::getMenuId, fySystemMenu -> fySystemMenu));
            for (FySystemShortcutMenu fySystemShortcutMenu : fySystemShortcutMenuList) {
                Long menuId = fySystemShortcutMenu.getMenuId();
                FySystemMenu fySystemMenu = systemMenuMap.get(menuId);
                if (fySystemMenu != null) {
                    fySystemShortcutMenu.setMenuIcon(fySystemMenu.getMenuIcon());
                    fySystemShortcutMenu.setMenuName(fySystemMenu.getMenuName());
                    fySystemShortcutMenu.setMenuPath(fySystemMenu.getMenuPath());
                }
            }
        }
        return Result.success(fySystemShortcutMenuList);
    }

    /**
     * 工作台快捷菜单信息编辑接口
     *
     * @param shortcutMenuUpdateList 快捷菜单信息
     * @return 操作结果
     */
    @Override
    public Result<String> update(List<ShortcutMenuUpdateVo> shortcutMenuUpdateList) {
        log.info(JSONArray.toJSONString(shortcutMenuUpdateList));
        //先清除原有配置
        Long userId = JwtTokenUtil.getLoginUserId();
        LambdaQueryWrapper<FySystemShortcutMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FySystemShortcutMenu::getUserId, userId);
        fySystemShortcutMenuMapper.delete(queryWrapper);
        if (CollectionUtils.isEmpty(shortcutMenuUpdateList)) {
            return Result.success();
        }
        for (ShortcutMenuUpdateVo shortcutMenuUpdateVo : shortcutMenuUpdateList) {
            Long menuId = shortcutMenuUpdateVo.getMenuId();
            boolean checkUserMenu = fySystemMenuService.checkUserMenu(menuId, userId);
            if (!checkUserMenu) {
                return Result.getResult(ResultCodeEnum.WORKBENCH_USER_NOT_MENU_PERMISSION);
            }
            FySystemShortcutMenu fySystemShortcutMenu = new FySystemShortcutMenu();
            fySystemShortcutMenu.setMenuId(menuId);
            fySystemShortcutMenu.setMenuColor(shortcutMenuUpdateVo.getMenuColor());
            fySystemShortcutMenu.setUserId(userId);
            fySystemShortcutMenu.setSort(shortcutMenuUpdateVo.getSort());
            fySystemShortcutMenuMapper.insert(fySystemShortcutMenu);
        }
        return Result.success();
    }
}
